//
//  ACNumberViewController.m
//  CP20180523
//
//  Created by apple on 2018/5/25.
//  Copyright © 2018年 apple. All rights reserved.
//

#import "ACNumberViewController.h"
#import "ACNumberView.h"

@interface ACNumberViewController ()

@property (weak, nonatomic) IBOutlet UIView *randomNumberBox;
@property (weak, nonatomic) IBOutlet UILabel *totalCountLabel;


@end

@implementation ACNumberViewController
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"随机推荐";
    [self getRandomNumber];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(reloadRandomNumberBox) name:@"reloadRandomNumberBox" object:nil];
}

-(void)getRandomNumber{
    for (UIView* view in self.randomNumberBox.subviews) {
        [view removeFromSuperview];
    }
    for (int i = 0; i<5; i++) {
        ACNumberView* numberView = [[UINib nibWithNibName:@"ACNumberView" bundle:[NSBundle mainBundle]] instantiateWithOwner:nil options:nil][0];
        numberView.frame = CGRectMake(0, 40*i, self.randomNumberBox.frame.size.width, 40);
        NSArray* numberArr = [[SelectionNumManager shared] randomRedNumberActionWithLottoryID:@"7"];
        [numberView getNumber:numberArr];
        [self.randomNumberBox addSubview:numberView];
    }
    self.totalCountLabel.text = [NSString stringWithFormat:@"共选 %ld 注",[self.randomNumberBox.subviews count]];
}

-(void)reloadRandomNumberBox{
    NSMutableArray* subViewsArray = [NSMutableArray arrayWithCapacity:0];
    [subViewsArray addObjectsFromArray:self.randomNumberBox.subviews];
    for (UIView* view in self.randomNumberBox.subviews) {
        [view removeFromSuperview];
    }
    for (int i = 0; i<[subViewsArray count]; i++) {
        ACNumberView* numberView = subViewsArray[i];
        numberView.frame = CGRectMake(0, 40*i, self.randomNumberBox.frame.size.width, 40);
        [self.randomNumberBox addSubview:numberView];
    }
    self.totalCountLabel.text = [NSString stringWithFormat:@"共选 %ld 注",[self.randomNumberBox.subviews count]];
}


- (IBAction)changeAction:(id)sender {
    [self getRandomNumber];
}

#pragma mark - 下一步
- (IBAction)nextAction:(id)sender {
    for (ACNumberView* view in self.randomNumberBox.subviews) {
        NSArray* numberArray = [view.numberLabel.text componentsSeparatedByString:@"  "];
        NSLog(@"%@",[numberArray componentsJoinedByString:@","]);
        CollectionNumberModel *model1 = [CollectionNumberModel setModelWithType:@"7" number:[numberArray componentsJoinedByString:@","]];
        
        [CollectionNumberManager collectionNumberWithArray:@[model1]];
    }
    
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
